CROSS_COMPILE	:= aarch64-none-elf-
CC				:= $(CROSS_COMPILE)gcc
LD				:= $(CROSS_COMPILE)ld
OC				:= $(CROSS_COMPILE)objcopy
QEMU			:= qemu-system-aarch64

MFLAGS	:= -march=armv8-a -mtune=cortex-a72 
CFLAGS 	:= $(MFLAGS) -Wall -ffreestanding -nostdinc -nostdlib

QEMU_FLAGS := -M virt -cpu cortex-a72 -kernel kernel.bin -nographic

SRCS := $(wildcard *.c)
OBJS := $(SRCS:.c=.o)

.PHONY: all clean kernel run

all: clean kernel

start.o: start.S
	$(CC) $(CFLAGS) -c start.S -o start.o

%.o: %.c
	$(CC) $(CFLAGS) -c $< -o $@

kernel: start.o $(OBJS)
	$(LD) -nostdlib start.o $(OBJS) -T link.ld -o kernel.elf
	$(OC) -O binary kernel.elf kernel.bin

clean:
	-rm *.o *.elf *.bin

run:
	$(QEMU) $(QEMU_FLAGS)
